package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;


	/**
	 * 根据以及分类id获取三级分类名称
	 * @param category1id
	 * @return
	 */
	@Override
	public List<BaseCategory3> findTopBaseCategory3(Long category1id) {
		//调用mapper
		//先查二级
		List<Long> categroy2IdList = baseCategory2Mapper.selectList(new LambdaQueryWrapper<BaseCategory2>()
				.eq(BaseCategory2::getCategory1Id, category1id)
		).stream().map(BaseCategory2::getId).collect(Collectors.toList());
		//在查三级
		List<BaseCategory3> category3List = baseCategory3Mapper.selectList(new LambdaQueryWrapper<BaseCategory3>()
				.in(BaseCategory3::getCategory2Id, categroy2IdList)
				.eq(BaseCategory3::getIsTop,1)
				.last(" limit 7 "));
		return category3List;
	}

	/**
	 * 获取一级分类下的所有分类
	 * @param category1id
	 * @return
	 */
	@Override
	public JSONObject getbaseCategoryList(Long category1id) {
		//new一个josn对象
		JSONObject categroy1 = new JSONObject();
		//先把一级分类数据放进去
		categroy1.put("categoryId",category1id);
		//根据一级分类id获取二级分类数据
		List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(
				new LambdaQueryWrapper<BaseCategoryView>().eq(BaseCategoryView::getCategory1Id, category1id)
		);
		//存储一级分类名称
		String category1Name = baseCategoryViews.get(0).getCategory1Name();
		categroy1.put("categoryName",category1Name);
		//获取二级分类数据(根据二级分类id分组)
		Map<Long, List<BaseCategoryView>> categroy2Map
				= baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
		//遍历赋值
		Iterator<Map.Entry<Long, List<BaseCategoryView>>> categroy2Iterator = categroy2Map.entrySet().iterator();
		//二级分类数组
		List<JSONObject> categroy2List = new ArrayList<>();
		while (categroy2Iterator.hasNext()) {
			JSONObject categroy2 = new JSONObject();
			Map.Entry<Long, List<BaseCategoryView>> entry = categroy2Iterator.next();
			//获取二级分类名称
			List<BaseCategoryView> category2Views = entry.getValue();
			String category2Name = category2Views.get(0).getCategory2Name();
			categroy2.put("categoryName",category2Name);
			//获取二级分类id
			Long categroy2Id = entry.getKey();
			categroy2.put("categoryId",categroy2Id);
			//获取三级分类数据列表
			List<JSONObject> catetroy3List = category2Views.stream().map(baseCategoryView -> {
				JSONObject categroy3 = new JSONObject();
				Long category3Id = baseCategoryView.getCategory3Id();
				String category3Name = baseCategoryView.getCategory3Name();
				//封装
				categroy3.put("categoryName", category3Name);
				categroy3.put("categoryId", category3Id);

				return categroy3;
			}).collect(Collectors.toList());
			categroy2.put("categoryChild",catetroy3List);
			//放进二级数组
			categroy2List.add(categroy2);
		}
		//放进一级
		categroy1.put("categoryChild",categroy2List);
		return categroy1;
	}

	/**
	 * 根据三级分类id获取分类id
	 * @param categroy3Id
	 * @return
	 */
	@Override
	public BaseCategoryView getCategoryView(Long categroy3Id) {
		BaseCategoryView baseCategoryView = baseCategoryViewMapper.selectById(categroy3Id);
		return baseCategoryView;
	}

	/**
	 * 获取专辑的属性和对应的属性值
	 * @param category1Id
	 * @return
	 */
	@Override
	public List<BaseAttribute> findAttribute(Long category1Id) {
		//调用mapper层的方法
		return baseAttributeMapper.selectAttribute(category1Id);
	}

	@Override
	public List<JSONObject> getBaseCategoryList() {
		/**这是List<JSONObject>里JSONObject的格式，要有：categoryName、categoryId、list<categoryChild>
		 * {
		 *     "categoryName":"音乐",   #一级分类名称
		 *     "categoryId":1,                       #一级分类ID
		 *     "categoryChild":[                     #当前一级分类包含的二级分类集合
		 *         {
		 *             "categoryName":"音乐音效",     #二级分类名称
		 *             "categoryId":101,               #二级分类ID
		 *             "categoryChild":[             #当前二级分类包含的三级分类集合
		 *                {
		 *                   "categoryName": "催眠音乐",
		 *                   "categoryId": 1001
		 *                 },
		 *                 {
		 *                   "categoryName": "放松音乐",
		 *                   "categoryId": 1002
		 *                 },
		 *                 {
		 *                   "categoryName": "提神音乐",
		 *                   "categoryId": 1003
		 *                 }
		 *             ]
		 *         }
		 *     ]
		 * }
		 */
		List<JSONObject> result = new ArrayList<>();
		//从视图中查询所有数据
		List<BaseCategoryView> baseCategoryViewsList = baseCategoryViewMapper.selectList(null);
		//按照以及分类id对数据进行分组。
		Map<Long, List<BaseCategoryView>> basecategroy1Map = baseCategoryViewsList.stream().collect(
				Collectors.groupingBy(BaseCategoryView::getCategory1Id)
		);
		//basecategroy1Map是一个map，key是Category1Id，value是List<BaseCategoryView>
		//拼凑返回值List<JSONObject>，遍历map返回
		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = basecategroy1Map.entrySet().iterator();
		while (iterator.hasNext()) {
			//获取返回值
			Map.Entry<Long, List<BaseCategoryView>> entry = iterator.next();
			//构造返回值,这个JSONObject继承了map接口，所以是个map，用put赋值
			JSONObject jsonObject = new JSONObject();
			//从entry获取valueList<BaseCategoryView>的第一个值值的name因为是想通过的。
			jsonObject.put("categoryName",entry.getValue().get(0).getCategory1Name());
			jsonObject.put("categoryId",entry.getKey());
			//获取二级分类数据
			List<BaseCategoryView> categoryViewsList = entry.getValue();
			//根据二级分类id分组
			Map<Long, List<BaseCategoryView>> category2Map = categoryViewsList.stream().collect(
					Collectors.groupingBy(BaseCategoryView::getCategory2Id)
			);
			//
			List<JSONObject> categoryChild = new ArrayList<>();			//再次遍历
			Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator1 = category2Map.entrySet().iterator();
			while (iterator1.hasNext()) {
				//获取数据
				Map.Entry<Long, List<BaseCategoryView>> entry1 = iterator1.next();
				//获取二级分类id
				Long categroy2Id = entry1.getKey();
				//获取二级分类下的数据
				List<BaseCategoryView> categoryViews = entry1.getValue();
				//构造二级的结构
				JSONObject jsonObject2 = new JSONObject();
				//给二级数据赋值
				jsonObject2.put("categoryName",categoryViews.get(0).getCategory2Name());
				jsonObject2.put("categoryId",categroy2Id);
				//三级没有重复不用分组
				List<JSONObject> categroy3List = categoryViews.stream().map(baseCategoryView -> {
					JSONObject categroy3 = new JSONObject();
					categroy3.put("categoryName", baseCategoryView.getCategory3Name());
					categroy3.put("categoryId", baseCategoryView.getCategory3Id());

					return categroy3;
				}).collect(Collectors.toList());
				//遍历三级数据
				jsonObject2.put("categoryChild",categroy3List);
				categoryChild.add(jsonObject2);
			}
			//二级分类放到一级分类中
			jsonObject.put("categoryChild",categoryChild);

			result.add(jsonObject);
		}

		return result;
		/*List<JSONObject> reslut = new ArrayList<>();
		List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(null);
		//按照一级id分组
		Map<Long, List<BaseCategoryView>> categroey1Map = baseCategoryViews.stream()
				.collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));

		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = categroey1Map.entrySet().iterator();
		JSONObject jsonObject = new JSONObject();
		while (iterator.hasNext()) {
			Map.Entry<Long, List<BaseCategoryView>> categroey1 = iterator.next();
			Long key1 = categroey1.getKey();
			List<BaseCategoryView> categoryViewsList = categroey1.getValue();
			jsonObject.put("categoryName",categoryViewsList.get(0).getCategory1Name());
			jsonObject.put("categoryId",key1);

			Map<Long, List<BaseCategoryView>> categroey2Map = categoryViewsList.stream()
					.collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator1 = categroey2Map.entrySet().iterator();

			JSONObject jsonObject2 = new JSONObject();
			while (iterator1.hasNext()) {
				Map.Entry<Long, List<BaseCategoryView>> entry = iterator1.next();
				Long key = entry.getKey();
				List<BaseCategoryView> categoryViews = entry.getValue();
				jsonObject2.put("categoryName",categoryViews.get(0).getCategory2Name());
				jsonObject2.put("categoryId",key);


			}
			jsonObject.put("categoryChild",jsonObject2);
		}*/

//		return null;
	}
}
